// myself/daily/Work.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    filename: null,
    filePath: null,
    fileId: null,
    dt: '',
    enddt:'',
    syschaosong:'',
    syschaosongid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toast = this.selectComponent("#toast");
    //获取后台管理员
   
    network.postJson(this, api.getAdminUser).then(res => {
      this.setData({
        syschaosongid: res.data.data.id,
        syschaosong: res.data.data.name
      })
    })
  },

  upload() {
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles
        let url = app.globalData.debug ? api.debugUrl : api.url
        wx.uploadFile({
          url: url + api.upFile, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          formData: {
            adminid: app.globalData.userInfo.uid,
            token: app.globalData.userInfo.token,
            cfrom: "weixin",
            device: app.globalData.device
          },
          success(res) {
            const data = res.data
            console.log(JSON.parse(res.data).id)
            that.data.fileId = JSON.parse(res.data).id
            that.setData({
              filename: JSON.parse(res.data).filename
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseType() {
    let that = this;
    wx.showActionSheet({
      itemList: this.data.days,
      success(res) {
        that.setData({
          day: that.data.days[res.tapIndex]
        })
      }
    })

  },
  bindDateDtChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dt: e.detail.value
    })
  },

  bindDateEndDtChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      enddt: e.detail.value
    })
  },

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
 if (!this.data.dt) {
      this.toast.showToast('请选择日期');
      return;
 } else if (!e.detail.value.content) {
      this.toast.showToast('请填写内容');
      return;
    } 
    network.postJson(this, api.worksave, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      id: 0,
      dt: this.data.dt,
      enddt: this.data.enddt,
      adddt: formatDate.formatTime(new Date(), "yyyy-MM-dd HH:mm:ss"),
      type:0,
      fileid: this.data.fileId,
      sysmodenum: 'daily',
      sysmodeid: '3',
      syschaosong: this.data.syschaosong,
      syschaosongid: this.data.syschaosongid,
      content: e.detail.value.content,
      plan: e.detail.value.plan,
    }).then(res => {
      this.toast.showToast('提交成功');
      wx.navigateBack({ changed: true });
    });
  }
})